package com.cqc.transition01;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cui on 2017/7/21.
 */

public class AdapterViewFlipperActivity extends AppCompatActivity {
    private Context context = this;
    private int[] imgArray = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f};


    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.adapterViewFlipper)
    android.widget.AdapterViewFlipper adapterViewFlipper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter_view_flipper);
        ButterKnife.bind(this);

        Property<View, Float> translationX = View.TRANSLATION_X;
        ObjectAnimator in = ObjectAnimator.ofFloat(adapterViewFlipper, translationX, -50, 0);
        ObjectAnimator out = ObjectAnimator.ofFloat(adapterViewFlipper, translationX, 0,200);
        adapterViewFlipper.setInAnimation(in);
        adapterViewFlipper.setOutAnimation(out);

        MyAdapter adapter = new MyAdapter();
        adapterViewFlipper.setAdapter(adapter);
    }

    @OnClick({R.id.btn1, R.id.btn2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                adapterViewFlipper.showPrevious();
                break;
            case R.id.btn2:
                adapterViewFlipper.showNext();
                break;
        }
    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return imgArray.length;
        }

        @Override
        public Object getItem(int position) {
            return imgArray[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(context);
            iv.setImageResource(imgArray[position]);
            return iv;
        }
    }

}
